using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Product : System.Web.UI.UserControl
{
    processProduct pProduct;
    processProductType pProductType;   
    
    protected void Page_Load(object sender, EventArgs e)
    {
                
        pProduct = new processProduct();
        pProductType = new processProductType();

        if (ViewState["flag"] == null)
        {
            ViewState["flag"] = "add";
        }


        if (!Page.IsPostBack)
        {
            Load_ddlproductype();
            
        }      
                 btnUpdate.Visible = false;
                Load_DatagvwProduct();
    }

    private void Load_ddlproductype()
    {
        ddlProductType.DataSource = pProductType.getProductTypes();
        ddlProductType.DataTextField = "ProductTypeName";
        ddlProductType.DataValueField = "ProductTypeID";
        ddlProductType.DataBind();
    }

    private void Load_DatagvwProduct()
    {
        gvwProduct.DataSource = pProduct.getProduct();
        gvwProduct.DataBind();
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        string imageName;
        if (FileUpload1.FileName != "")
        {
            imageName = Server.MapPath("..\\Images\\" + FileUpload1.FileName);
            FileUpload1.PostedFile.SaveAs(imageName);
            imageName = FileUpload1.FileName;
        }
        else
        {
            imageName = "Empty.jpg";
        }

        string productID = pProduct.geProductID();
        string ProductName = txtProductName.Text;
        string ProductDetail = txaProductDetail.Text;
        float ProductPrice = Convert.ToSingle(txtProductPrice.Text);
        string ProductWarranty = txtProductWarranty.Text;
        int ProductQuantity = Convert.ToInt32(txtProductQuantity.Text);
        string ProductType = ddlProductType.SelectedValue.ToString();

        if (ViewState["flag"].ToString().Trim().Equals("add"))
        {
            if (pProduct.insertProduct(productID, ProductName, imageName, ProductDetail, ProductPrice, ProductWarranty, ProductQuantity, ProductType) > 0)
            {
                Load_DatagvwProduct();
                Reset_Control();
                btnEdit.Visible = true;
                btnAdd.Visible = true;
            }
        }
        else
        {            
            string ProductID = txtProductID.Text;
            if (pProduct.updateProduct(ProductID,ProductName,imageName,ProductDetail,ProductPrice,ProductWarranty,ProductQuantity,ProductType)>0)
            {                
                btnUpdate.Visible = true;
                Reset_Control();
                btnEdit.Visible = false;
                btnAdd.Visible = false;                
                Load_DatagvwProduct();
            }
        }
    }

    private void Reset_Control()
    {
        txtProductName.Text = "";
        txaProductDetail.Text = "";
        txtProductPrice.Text = "";
        txtProductQuantity.Text = "";
        txtProductWarranty.Text = "";
    }
    
    protected void gvwProduct_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvwProduct.PageIndex = e.NewPageIndex;
        btnUpdate.Visible = true;
        Load_DatagvwProduct();
    }


    protected void gvwProduct_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        string ProductID = gvwProduct.DataKeys[e.NewSelectedIndex].Value.ToString();
        txtProductID.Text = ProductID.ToString();
        btnUpdate.Visible = true;
        btnAdd.Visible = false;
        btnEdit.Visible = false;
        Load_DataControl(ProductID);
        ViewState["flag"] = "edit";

    }

    private void Load_DataControl(string ProductID)
    {
        DataTable dtProduct = pProduct.getProductByID(ProductID);        
        txtProductName.Text = dtProduct.Rows[0]["ProductName"].ToString();
        txaProductDetail.Text = dtProduct.Rows[0]["ProductDetail"].ToString();
        txtProductPrice.Text = dtProduct.Rows[0]["ProductPrice"].ToString();            
        txtProductQuantity.Text = dtProduct.Rows[0]["ProductQuantity"].ToString();            
        txtProductWarranty.Text = dtProduct.Rows[0]["ProductWarranty"].ToString();            
        ddlProductType.SelectedValue = dtProduct.Rows[0]["ProductTypeID"].ToString();
        //ViewState["flag"] = "edit";
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Reset_Control();
        btnAdd.Visible = false;
        btnEdit.Visible = false;
        gvwProduct.Visible = true;
        Load_DatagvwProduct();
        btnUpdate.Visible = true;
        ViewState["flag"] = "add";
        
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        Reset_Control();
        btnAdd.Visible = false;
        btnEdit.Visible = false;
        gvwProduct.Visible = true;
        btnUpdate.Visible = true;
        Load_DatagvwProduct();
        ViewState["flag"] = "edit";
       
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        btnAdd.Visible = true;
        btnEdit.Visible = true;
        btnUpdate.Visible = false;
        gvwProduct.Visible = true;
        Load_DatagvwProduct();
        Reset_Control();
    }
    protected void gvwProduct_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            string ProductID = gvwProduct.DataKeys[e.RowIndex].Value.ToString();
            if (pProduct.deleteProduct(ProductID) > 0)
            {
                btnUpdate.Visible = true;
                Reset_Control();
                btnEdit.Visible = false;
                btnAdd.Visible = false;
                Load_DatagvwProduct();                
            }
            else
            {
                Alert.Show("This product is being ordered, you can not delete it!");
            }
        }
        catch (Exception)
        {
            Alert.Show("This product is being ordered, you can not delete it!");
        }
        
        
    }
    //protected void gvwProduct_RowDataBound(object sender, GridViewRowEventArgs e)
    //{
    //    if (e.Row.RowType == DataControlRowType.DataRow)
    //    {
    //        GridViewRow rowselected = e.Row;
    //        Button btnDelete = (Button)rowselected.Cells[4].Controls[0];
    //        btnDelete.Attributes.Add("Onclick", "if(confirmDelete()==false){return false;}");
    //        //Label1.Text = btnDelete.Text;
    //    }
    //}

    //protected void btnUpdateEdit_Click(object sender, EventArgs e)
    //{
    //    string ProductID = txtProductID.Text;
        
    //    if (pProduct.updateProduct(productID, ProductName, imageName, ProductDetail, ProductPrice, ProductWarranty, ProductQuantity, ProductType) > 0)
    //    {
    //        Reset_Control();
    //        btnEdit.Visible = false;
    //        btnAdd.Visible = false;
    //        btnUpdate.Visible = true;
    //        Load_DatagvwProduct();
    //    }
    //}
}
